$(document).ready(function() {
	//获取cookie
	function getCookie(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while(c.charAt(0) == ' ') c = c.substring(1);
			if(c.indexOf(name) != -1) return c.substring(name.length, c.length);
		}
		return "";
	}
	var tempUid = getCookie('uid');
	if(tempUid == '') {
		location.href = 'http://www.ibabypet.com.cn/pcpet/login.html'
	}
	console.log('tempUid', tempUid)
	var my_account = new Vue({
		created() {
			var self = this
			$.ajax({
				type: "get",
				url: "http://www.ibabypet.com.cn/public/index.php/api/area/area_list/",
				success: function(res) {
					res = JSON.parse(res);
					console.log('获取省市区res0', res)
					if(res.code == 200) {
						self.ProvinceList = res.data.area
					} else {
						alert(res.code)
					}
				}
			});
			$.ajax({
				type: "get",
				url: "http://www.ibabypet.com.cn/public/index.php/api/address/pcaddress_list/",
				data: {
					uid: tempUid
				},
				success: function(res) {
					res = JSON.parse(res);
					console.log('获取收货地址列表res', res)
					if(res.code == 200) {
						self.adr_list = res.data
						$.each(self.adr_list, function(i, v) {
							self.$set(v, 'provineCityCountry', v.province_name + v.city_name + v.country_name)
						});
						console.log('self.adr_list', self.adr_list)
					} else {
						alert(res.code)
					}
				}
			});
		},
		el: '#my_account',
		data: {
			adr_list: [],
			ruleForm: {
				name: '',
				detailAddress: '',
				phone: '',
				id_card: '',
				coding: '',
				isMoren: false,
			},
			addIsMoren: '',
			ProvinceList: [],
			CityList: [],
			AreaList: [],
			valueProvince: '',
			province_name: '',
			valueCity: '',
			city_name: '',
			valueArea: '',
			country_name: '',
			adrId: '',
			rules: {
				name: [{
					required: true,
					message: '请输入收货人姓名',
					trigger: 'blur'
				}],
				phone: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur'
					},
					{
						min: 11,
						max: 11,
						message: '请输入正确的手机号',
						trigger: 'blur',
					}
				],
				detailAddress: [{
					required: true,
					message: '请输入具体地址',
					trigger: 'blur'
				}],
				Province: [{
					required: true,
					message: '请选择省',
					trigger: 'blur'
				}]
			}
		},
		watch: {

		},
		methods: {
			changeProvince(valueProvince) {
				var self = this
				$.ajax({
					type: "get",
					url: "http://www.ibabypet.com.cn/public/index.php/api/area/area_list/",
					data: {
						area: valueProvince
					},
					success: function(res) {
						res = JSON.parse(res);
						if(res.code == 200) {
							self.CityList = res.data.area
						} else {
							alert(res.code)
						}
					}
				});
			},
			changeCity(valueCity) {
				var self = this
				$.ajax({
					type: "get",
					url: "http://www.ibabypet.com.cn/public/index.php/api/area/area_list/",
					data: {
						area: valueCity
					},
					success: function(res) {
						res = JSON.parse(res);
						if(res.code == 200) {
							self.AreaList = res.data.area
						} else {
							alert(res.code)
						}
					}
				});
			},
			changeArea(valueArea) {
				var self = this
				$.each(self.ProvinceList, function(i, v) {
					if(v.area_id == self.valueProvince) {
						self.province_name = v.area_name
					}
				});
				$.each(self.CityList, function(i, v) {
					if(v.area_id == self.valueCity) {
						self.city_name = v.area_name
					}
				});
				$.each(self.AreaList, function(i, v) {
					if(v.area_id == self.valueArea) {
						self.country_name = v.area_name
					}
				});
			},
			handleEdit: function(index, row) {
				console.log('index', index)
				console.log('row', row)
				var self = this
				self.valueProvince = row.province_name
				self.valueCity = row.city_name
				self.valueArea = row.country_name
				self.ruleForm.detailAddress = row.address
				self.ruleForm.name = row.name
				self.ruleForm.phone = row.phone
				self.adrId = row.id
				if(row.is_default == '1') {
					self.addIsMoren = true
				} else {
					self.addIsMoren = false
				}
				window.scrollTo(0, 0)
			},
			handleDelete: function(index, row) {
				var self = this
				this.$confirm('此操作将永久删除地址, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					$.ajax({
						type: "post",
						url: "http://www.ibabypet.com.cn/public/index.php/api/address/delete_pcaddress/",
						data: {
							id: row.id,
						},
						success: function(res) {
							res = JSON.parse(res);
							console.log('删除地址', res)
							if(res.code == 200) {
								self.$message(res.msg);
								location.reload()
							} else {
								self.$message(res.msg);
							}
						}
					});
				}).catch(() => {

				});

			},
			submitAddress(formName) {
				var self = this
				if(self.addIsMoren == true) {
					self.addIsMoren = 1
				} else {
					self.addIsMoren = 2
				}
				if(self.adrId == '') {
					this.$refs[formName].validate((valid) => {
						if(valid) {
							$.ajax({
								type: "post",
								url: "http://www.ibabypet.com.cn/public/index.php/api/address/add_pcaddress/",
								data: {
									uid: tempUid,
									province_id: self.valueProvince,
									city_id: self.valueCity,
									country_id: self.valueArea,
									province_name: self.province_name,
									city_name: self.city_name,
									country_name: self.country_name,
									address: self.ruleForm.detailAddress,
									name: self.ruleForm.name,
									phone: self.ruleForm.phone,
									coding: self.ruleForm.coding,
									is_default: self.addIsMoren,
									id_card: self.ruleForm.id_card,
								},
								success: function(res) {
									res = JSON.parse(res);
									console.log('添加地址返回的信息res', res)
									if(res.code == 200) {
										self.$message(res.msg);
										location.reload()
										console.log('提交添加地址信息后res.data', res.data)
									} else {
										self.$message(res.msg);
										console.log('error提交添加地址信息后res.data', res)
									}
								}
							});
						} else {
							console.log('error submit!!');
							return false;
						}
					});
				} else {
					this.$refs[formName].validate((valid) => {
						if(valid) {
							$.ajax({
								type: "post",
								url: "http://www.ibabypet.com.cn/public/index.php/api/address/up_pcaddress/",
								data: {
									id: self.adrId,
									province_id: self.valueProvince,
									city_id: self.valueCity,
									country_id: self.valueArea,
									province_name: self.province_name,
									city_name: self.city_name,
									country_name: self.country_name,
									address: self.ruleForm.detailAddress,
									name: self.ruleForm.name,
									phone: self.ruleForm.phone,
									coding: self.ruleForm.coding,
									is_default: self.addIsMoren,
									id_card: self.ruleForm.id_card,
								},
								success: function(res) {
									res = JSON.parse(res);
									console.log('修改地址返回的信息res', res)
									if(res.code == 200) {
										self.$message(res.msg);
										location.reload()
									} else {
										self.$message(res.msg);
									}
								}
							});
						} else {
							console.log('error submit!!');
							return false;
						}
					});
				}
			},

		}
	})

});